package com.campus.food.service.impl;

import com.campus.food.exception.BusinessException;
import com.campus.food.mapper.BannerMapper;
import com.campus.food.model.entity.Banner;
import com.campus.food.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 轮播图服务实现类
 * 
 * @author Claude3.7 Sonnet
 * @since 2025-06-12
 */
@Service
public class BannerServiceImpl implements BannerService {
    
    @Autowired
    private BannerMapper bannerMapper;
    
    @Override
    @Transactional
    public Banner addBanner(Banner banner) throws BusinessException {
        // 设置默认值
        if (banner.getSort() == null) {
            banner.setSort(0);
        }
        if (banner.getStatus() == null) {
            banner.setStatus(1); // 默认启用
        }
        
        // 设置创建和更新时间
        LocalDateTime now = LocalDateTime.now();
        banner.setCreateTime(now);
        banner.setUpdateTime(now);
        
        // 添加轮播图
        bannerMapper.insert(banner);
        
        return banner;
    }
    
    @Override
    public Banner getBannerById(Long id) throws BusinessException {
        Banner banner = bannerMapper.selectById(id);
        if (banner == null) {
            throw new BusinessException("轮播图不存在");
        }
        return banner;
    }
    
    @Override
    @Transactional
    public boolean updateBanner(Banner banner) throws BusinessException {
        // 检查轮播图是否存在
        if (bannerMapper.selectById(banner.getId()) == null) {
            throw new BusinessException("轮播图不存在");
        }
        
        // 设置更新时间
        banner.setUpdateTime(LocalDateTime.now());
        
        // 更新轮播图
        return bannerMapper.updateById(banner) > 0;
    }
    
    @Override
    @Transactional
    public boolean deleteBanner(Long id) throws BusinessException {
        // 检查轮播图是否存在
        if (bannerMapper.selectById(id) == null) {
            throw new BusinessException("轮播图不存在");
        }
        
        // 删除轮播图
        return bannerMapper.deleteById(id) > 0;
    }
    
    @Override
    public List<Banner> getAllBanners() {
        return bannerMapper.selectAll();
    }
    
    @Override
    public List<Banner> getActiveBanners() {
        return bannerMapper.selectByStatus(1);
    }
    
    @Override
    @Transactional
    public boolean updateBannerStatus(Long id, Integer status) throws BusinessException {
        // 检查轮播图是否存在
        if (bannerMapper.selectById(id) == null) {
            throw new BusinessException("轮播图不存在");
        }
        
        // 检查状态值是否合法
        if (status != 0 && status != 1) {
            throw new BusinessException("状态值不合法");
        }
        
        // 更新状态
        Banner banner = new Banner();
        banner.setId(id);
        banner.setStatus(status);
        banner.setUpdateTime(LocalDateTime.now());
        
        return bannerMapper.updateById(banner) > 0;
    }
    
    @Override
    public List<Banner> getBannerList(int page, int size, Integer status) {
        // 计算偏移量
        int offset = (page - 1) * size;
        
        return bannerMapper.selectPage(offset, size, status);
    }
    
    @Override
    public long getBannerCount(Integer status) {
        return bannerMapper.count(status);
    }
    
    @Override
    @Transactional
    public boolean deleteBannerBatch(List<Long> ids) throws BusinessException {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException("ID列表不能为空");
        }
        
        return bannerMapper.deleteBatch(ids) > 0;
    }
    
    @Override
    @Transactional
    public boolean updateBannerSort(Long id, Integer sort) throws BusinessException {
        // 检查轮播图是否存在
        if (bannerMapper.selectById(id) == null) {
            throw new BusinessException("轮播图不存在");
        }
        
        // 检查排序值是否合法
        if (sort < 0) {
            throw new BusinessException("排序值不能为负数");
        }
        
        // 更新排序
        Banner banner = new Banner();
        banner.setId(id);
        banner.setSort(sort);
        banner.setUpdateTime(LocalDateTime.now());
        
        return bannerMapper.updateById(banner) > 0;
    }
} 